package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.ioc.AbIocView;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.Gcwx_Person;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.abXMLGcwxPersonlist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;

/**
 * Created by lkp on 2015/11/29.
 */
//档案选择
public class Basenvo_Choosewxperson extends base_index {
    private ArrayList<Gcwx_Person> wxpersonlist;
    private String btntype;
    private String ccode;

    @AbIocView(id = R.id.btn_tv, click = "btnClick")
    TextView btn_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.basenvo_rchoose);
        initview("维修员");
        initview2();
    }

    public void btnClick(View v) {
        Intent intent = new Intent();
        //从position改为id,因为过滤之后的position不正确
        intent.putExtra("ccode", "");
        intent.putExtra("btntype", btntype);
        intent.putExtra("cname", "");
        intent.putExtra("cgcode", "");
        intent.putExtra("cgname", "");
        setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
        finish();
    }

    //初始化2
    @Override
    public void initview2() {
        //获取传递的信息
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取传入的数据，并使用Bundle记录
        btntype = bundle.getString("btntype");
        ccode = bundle.getString("ccode");
        wxpersonlist = myApplication.getGv_gcwxperson();
        if (wxpersonlist == null || wxpersonlist.size() < 1) {
            //获取存货列表
            getdata(0);
        } else {
            createrl();
        }

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Gcwx_Person wxperson = wxpersonlist.get((int) id);
                Intent intent = new Intent();
                //从position改为id,因为过滤之后的position不正确
                intent.putExtra("ccode", wxperson.getCwxpcode());
                intent.putExtra("btntype", btntype);
                intent.putExtra("cname", wxperson.getCwxpname());
                intent.putExtra("cgcode", wxperson.getCwxgcode());
                intent.putExtra("cgname", wxperson.getCwxgname());
                setResult(R.layout.basenvo_rchoose / 200000, intent);  //设置返回结果
                finish();
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
//        AbTask mAbTask = new AbTask();
//        final AbTaskItem item = new AbTaskItem();
//        item.setListener(new AbTaskListener() {
//
//            @Override
//            public void update() {
//                TreeMap<String, String> properties = new TreeMap<String, String>();
//                //参数
//                String[] parms = {"cacc_id", "iyear"};
//                String[] vals = {(String) myApplication.getGs_ztid(), (String) myApplication.getGs_year()};
//                //生成xml
//                BuildQgdListXMLService service = new BuildQgdListXMLService();
//                String xmlstring = service.buildXML(null, "gcwxpersonlist", InstorageURL.NAME_SPACE, parms, vals, null);
//
//                ToolSOAPxml.callService("gcwxpersonlist", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//                    @Override
//                    public void onSucced(String result) {
//                        //解析xml文件,返回列表
//                        if (result != null) {
//                            wxpersonlist = XMLGcwxPersonlist.paraseCommentInfors(result);
//                            myApplication.setGv_gcwxperson(wxpersonlist);
//                            createrl();
//                        } else {
//                            AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                        }
//                    }
//
//                    @Override
//                    public void onFailure(String result) {
//                        AbToastUtil.showToastInThread(getApplicationContext(), R.string.sqlerrormsg);
//                        //swipeRefreshLayout.setRefreshing(false);
//                        //ToolLog.log("err：" + result);
//                    }
//                });
//            }
//
//            @Override
//            public void get() {
//                try {
//                    Thread.sleep(1000);
//                } catch (Exception e) {
//                }
//            }
//        });
//        mAbTask.execute(item);

        String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
        AbSoapParams params = new AbSoapParams();
        params.put("arg0", myApplication.getGs_ztid());
        params.put("arg1", myApplication.getGs_year());
        mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "gcwxpersonlist", params, new AbSoapListener() {
            //获取数据成功会调用这里
            @Override
            public void onSuccess(int statusCode, String content) {
                if (content != null) {
                    wxpersonlist = abXMLGcwxPersonlist.paraseCommentInfors(content);
                    myApplication.setGv_gcwxperson(wxpersonlist);
                    createrl();
                } else {
                    AbToastUtil.showToast(Basenvo_Choosewxperson.this, R.string.sqlerrormsg);
                }
            }

            // 失败，调用
            @Override
            public void onFailure(int statusCode, String content,
                                  Throwable error) {
                AbToastUtil.showToast(Basenvo_Choosewxperson.this, R.string.sqlerrormsg);
            }

            // 开始执行前
            @Override
            public void onStart() {
                //显示进度框
                AbDialogUtil.showProgressDialog(Basenvo_Choosewxperson.this, 0, "正在加载...");
            }

            // 完成后调用，失败，成功
            @Override
            public void onFinish() {
                //移除进度框
                AbDialogUtil.removeDialog(Basenvo_Choosewxperson.this);
            }
        });
    }

    public void createrl() {
        adatperDataList.clear();
        ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < wxpersonlist.size(); i++) {
            Gcwx_Person wxperson = new Gcwx_Person();
            wxperson = wxpersonlist.get(i);
            //增加一个行选择状态初始化
            if (ccode != null && wxperson.getCwxpcode().equals(ccode)) {
                sa.getIsSelected().put(i, true);
            } else {
                sa.getIsSelected().put(i, false);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("index", (i + 1));
            map.put("cvouchid", "维修组名称：" + wxperson.getCwxgname());
            map.put("cdept", "维修员编号：" + wxperson.getCwxpcode());
            map.put("cnocheckreason", "维修员名称：" + wxperson.getCwxpname());
            map.put("ddate", "维修员电话：" + wxperson.getCwxptel());
            map.put("more", "0");
            dataListForInit.add(map);
        }
        toolDataProvider.initData(dataListForInit);
        loadList();
        //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
        //swipeRefreshLayout.setRefreshing(false);
    }
}
